package com.tuanzi.loan.web.bean.xhh.settlementsystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementCompany;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementIncome;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementIncomeActivity;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementPrepay;
import com.tuanzi.loan.business.service.xhh.settlementsystem.*;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementRemainRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementRemainResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.xhh.settlementsystem.XhhSettlementRemainLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:52
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class XhhSettlementRemainListBean extends JSFListingBean<XhhSettlementRemainRequest, XhhSettlementRemainResponse> {
    private static final long serialVersionUID = -2015508705892452521L;
    @Autowired
    private XhhSettlementCompanyService xhhSettlementCompanyService;

    @Autowired
    private XhhSettlementRemainService xhhSettlementRemainService;

    @Autowired
    private XhhSettlementPrepayService xhhSettlementPrepayService;

    @Autowired
    private XhhSettlementIncomeService xhhSettlementIncomeService;

    @Autowired
    private XhhSettlementIncomeActivityService xhhSettlementIncomeActivityService;

    private List<XhhSettlementCompany> companyList;

    private XhhSettlementIncome settlementIncome;

    private XhhSettlementPrepay settlementPrepay;

    private List<String> activityNameList;
    @PostConstruct
    public void init() throws Exception {
        companyList = xhhSettlementCompanyService.getCompanyList();
        settlementIncome = new XhhSettlementIncome();
        settlementPrepay = new XhhSettlementPrepay();
        activityNameList = Lists.newArrayList();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new XhhSettlementRemainRequest();
        lazyDataModel = new XhhSettlementRemainLazyDataModel($);
    }

    public void onReset() throws Exception{
        initLazyDataModel();
    }

    public void onSeeRelateRecord(Long relateId,Integer remainType){
        if (remainType.equals(XhhRemainType.PREPAY.index)){//预付
            settlementPrepay = xhhSettlementPrepayService.findOne(relateId);
        }
        else if (remainType.equals(XhhRemainType.EXPEND.index)){//消耗
            settlementIncome = xhhSettlementIncomeService.findOne(relateId);
            activityNameList = xhhSettlementIncomeActivityService.findAllByIncomeId(relateId).parallelStream()
                    .map(XhhSettlementIncomeActivity::getActivityName).collect(Collectors.toList());
        }

    }

    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(XHH_SETTLEMENT_SPREAD_CHANNEL,XHH_SETTLEMENT_INCOME_TYPE,
                XHH_SETTLEMENT_SPREAD_MODE, XHH_COOPERATION_MODE,
                XHH_SETTLEMENT_CYCLE, XHH_SETTLEMENT_PAY_MODE);
    }
}
